package com.sto.common.importExcel.factory;

import com.alibaba.fastjson.JSON;
import com.sto.common.importExcel.dubbo.service.AbstractExcelService;
import com.sto.common.importExcel.dubbo.service.AbstractExportExcelService;
import com.sto.common.importExcel.dubbo.service.AbstractImportExcelService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * Excel处理工厂
 * 注意：交给spring管理
 *
 * @author ChenPeng
 * @since 2022-07-15 11:32
 */
@Slf4j
@Component
public final class ExcelHandleFactory implements ApplicationContextAware, InitializingBean {

    public static final String DEFAULT_PARAMETER = "excelType";
    /**
     * 策略类工厂map
     */
    public static final Map<String, Class<AbstractExcelService>> STRATEGY_BEAN_MAP
            = new HashMap<String, Class<AbstractExcelService>>(16);
    public static final Map<String, Class<AbstractExportExcelService>> EXPORT_STRATEGY_MAP
            = new HashMap<String, Class<AbstractExportExcelService>>(16);
    public static final Map<String, Class<AbstractImportExcelService>> IMPORT_STRATEGY_MAP
            = new HashMap<String, Class<AbstractImportExcelService>>(16);

    public static final Map<String, String> IMPORT_STRATEGY_TYPE = new HashMap<String, String>(16);

    @Resource
    private ApplicationContext applicationContext;

    private static ExcelHandleFactory instance;



    @Override
    public void afterPropertiesSet() throws Exception {

    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        log.info("strategyBeanMap size:{},strategyBeanMap:{}", STRATEGY_BEAN_MAP.size(),
                JSON.toJSONString(STRATEGY_BEAN_MAP.keySet()));
    }


    /**
     * 获取导入执行器
     */
    public static AbstractImportExcelService getImportExcelService(String excelType) {
        Class<AbstractImportExcelService> excelServiceClass = IMPORT_STRATEGY_MAP.get(excelType.toLowerCase());
        return instance.applicationContext.getBean(excelServiceClass);
    }
}
